package com.odais.DataManagement;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.odais.Reservation.Reservation;
import com.odais.Reservation.ReservationList;
import com.odais.UserManagement.UserManager;
import com.odais.json.JSON_IO_Interface;

public class GetReservationListOnServer extends AsyncTask<Void, String, Void> 
{
	// ===========================================================
	// Constants
	// ===========================================================
	private static final String STATUS_PENDING = "1000";


	// ===========================================================
	// Fields
	// ===========================================================
	private Context mContext;
	private ReservationList mReservationList;
	private ArrayList<DataListener> mListeners;

	// ===========================================================
	// Constructors
	// ===========================================================

	public GetReservationListOnServer(Context context, ReservationList reservationList, ArrayList<DataListener> listener) {
		super();
		this.mContext = context;
		this.mReservationList = reservationList;
		this.mListeners = listener;
		
	}
		
	// ===========================================================
	// Getter & Setter
	// ===========================================================


	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
		
	@Override
	protected void onPreExecute()
    { 

    }

	@Override
    protected Void doInBackground(Void... unused)
	{

    	String JSON_Buffer[] = new String[10];
    	HashMap<String, ?> JSON_BufferOut;

    	//{"action":"getResDB","key":"seller","value":"1452"}
    	JSON_Buffer[0] = "action";
    	JSON_Buffer[1] = "getResDB";
    	JSON_Buffer[2] = "key";
    	JSON_Buffer[3] = "seller";
    	JSON_Buffer[4] = "value";
    	JSON_Buffer[5] = UserManager.getUserID();
    	JSON_Buffer[6] = "key2";
    	JSON_Buffer[7] = "status";
    	JSON_Buffer[8] = "value2";
    	JSON_Buffer[9] = STATUS_PENDING;

    	JSON_IO_Interface jsonPost = new JSON_IO_Interface();
    	JSON_BufferOut = (HashMap<String, ?>) jsonPost.JSON_Post(JSON_Buffer);
        getItems(JSON_BufferOut);

        return(null);
    }

    @Override
    protected void onProgressUpdate(String... item) 
    {
    	//mProgressBar.set
    }

    @Override
    protected void onPostExecute(Void unused)
    {		    	
      Toast
        .makeText(mContext, "Done!", Toast.LENGTH_SHORT)
        .show();
      
    }
    


	private void getItems(HashMap<String, ?> JSON_BufferOut)
	{
        try
        {
        	mReservationList =  new ReservationList();
        	mReservationList.create();
        	
        	ReservationRepository reservationRepository = new ReservationRepository(mContext);

            //get controller's arraylist

            HashMap<String, String> datahash = new HashMap<String, String>();
            
            reservationRepository.Open();
            for(int i=0; i<JSON_BufferOut.size(); i++){
            	datahash = (HashMap<String, String>) JSON_BufferOut.get(Integer.toString(i));

    	        Reservation tmpReservation = new Reservation("El Tchitcho",
   					 "Buggy",
   					 3,
   					Integer.parseInt(datahash.get("status")),
   					datahash.get("numrsv").toString(),
   					Integer.parseInt(datahash.get("numtbl")),
   					datahash.get("heurdv").toString(),
   					i,
   					"");
    	        mReservationList.add(tmpReservation);
    	        reservationRepository.Save(tmpReservation);
            }
            reservationRepository.Close();

            for (DataListener listener  : mListeners) 
            {
                listener.onDataUpdate(mReservationList);
            }
          } catch (Exception e) {
          }
      }

}
